package widget.common.utils
{
	import flash.utils.ByteArray;

	public class StringUtil
	{
		//纯文字
		public static const WORDS_TXT : int = 1;
		//插入网路图片
		public static const WEB_IMAGE : int = 3;
		//插入系统表情
		public static const FACE_IMAGE : int = 2;

		public static function parseURL(url : String) : String
		{
			var configURL : String = "http://";
			var urlList : Array = url.split("[[IMPORT]]/");
			var urlArray : Array = null;
			if (urlList && urlList.length > 1)
			{
				urlArray = urlList[1].split("/");
				if (urlArray && urlArray.length > 0)
				{
					for (var i : int = 0 ; i < urlArray.length - 1 ; i++)
					{
						configURL += urlArray[i] + "/";
					}
				}
			}
			else
			{
				urlArray = url.split("/");
				if (urlArray && urlArray.length > 1)
				{
					for (var j : int = 2 ; j < urlArray.length - 1 ; j++)
					{
						configURL += urlArray[j] + "/";
					}
				}
			}
			return configURL;
		}

		/**
		 * 自动识别需要转义的字符的方法 ("<",">")
		 * @param str 收到的信息字符串
		 * @return 返回识别需要转义的字符后的字符串
		 *
		 */
		public static function parseSpecil(str : String) : String
		{
			if (str == null)
				return null;
			var reg : RegExp;
			reg = new RegExp("<" , "gi");
			str = str.replace(reg , "&lt;");
			reg = new RegExp(">" , "gi");
			str = str.replace(reg , "&gt;");
			return str;
		}

		/**
		 * 清空信息换行符
		 * @return 信息字符串
		 *
		 */
		public static function clearEnter(msg : String) : String
		{
			var tempArr : Array = msg.split("\r");
			var myMsg : String = "";
			for (var i : int = 0 ; i < tempArr.length ; i++)
			{
				myMsg += tempArr[i];
			}
			return myMsg;
		}

		/**
		 * 清空空格符
		 * @return 信息字符串
		 *
		 */
		public static function clearSpace(msg : String) : String
		{
			if (msg == null)
				return null;
			var tempArr : Array = msg.split(" ");
			var myMsg : String = "";
			for (var i : int = 0 ; i < tempArr.length ; i++)
			{
				myMsg += tempArr[i];
			}
			return myMsg;
		}

		/**
		 * 压缩方法
		 * @param content 压缩内容
		 * @return 返回压缩后的二进制字符串
		 *
		 */
		public static function compress(content : String) : String
		{
			var byteArr : ByteArray = new ByteArray();
			var str : String = "";
			byteArr.writeUTF(content);
			byteArr.compress();
			for (var i : int = 0 ; i < byteArr.length ; i++)
			{
				str += (Number(byteArr[i]).toString() + "#");
			}
			return str;
		}

		/**
		 * 解压缩方法
		 * @param content 解压缩内容
		 * @return 返回解压缩后的二进制字符串
		 *
		 */
		public static function uncompress(content : String) : String
		{
			var byteArr : ByteArray = new ByteArray();
			var tempArr : Array = content.split("#");
			for (var j : int = 0 ; j < tempArr.length ; j++)
			{
				byteArr[j] = Number(tempArr[j]);
			}
			byteArr.uncompress();
			return byteArr.readUTF();
		}

		/**
		 * 解析一个字符串里是否包含了图片或者表情
		 * @param message
		 * @return
		 *
		 */
		public static function parsePhiz(message : String) : Array
		{
			var txt : String = message;
			var dividerStr : String = "(:|";
			var tempList : Array = txt.split(dividerStr);
			var resultList : Array = new Array();
			for (var i : int = 0 ; i < tempList.length ; i++)
			{
				if (i == 0)
				{
					resultList.push({label: tempList[i] , type: WORDS_TXT});
				}
				else
				{
					var s1 : String = tempList[i].toString()
					if (s1.indexOf(')') != -1)
					{
						var s2 : String = s1.substring(0 , s1.indexOf(')'));
						if (!isNaN(Number(s2)))
						{
							//is number , is face number ;
							var tagName : String = "Face" + s2
							if (tagName)
							{
								resultList.push({label: tagName , type: FACE_IMAGE});

								var s3 : String = s1.substring(s1.indexOf(')') + 1 , s1.length);
								resultList.push({label: s3 , type: WORDS_TXT});
							}
							else
							{
								resultList.push({label: dividerStr + s1 , type: WORDS_TXT});
							}
						}
						else
						{
							if (s2.substring(0 , 7) == 'http://')
							{
								//insert image
								resultList.push({label: s2 , type: WEB_IMAGE});

								s3 = s1.substring(s1.indexOf(')') + 1 , s1.length);
								resultList.push({label: s3 , type: WORDS_TXT});
							}
							else
							{
								resultList.push({label: dividerStr + s1 , type: WORDS_TXT});
							}
						}
					}
					else
					{
						resultList.push({label: dividerStr + s1 , type: WORDS_TXT});
					}
				}

			}
			return resultList;
		}

		public static function formatDate(time : Number , hour : Boolean = false) : String
		{
			var date : Date = new Date(time);
			var month : int = date.getMonth() + 1;
			var day : int = date.getDate();
			var year : int = date.getFullYear();
			var lab : String = null;
			if (hour)
			{
				var hou : int = date.getHours();
				var min : int = date.getMinutes();
				lab = month + "/" + day + " " + hou + ":" + min;
				return lab;
			}
			else
			{
				lab = year + "-" + month + "-" + day;
				return lab;
			}
		}

		/**
		 * 自动识别URL的方法 (www.|http://|ftp://|xx@xx.xx|mms://)
		 * @param str 收到的信息字符串
		 * @return 返回识别url后的字符串
		 *
		 */
		public static function urlParser(str : String) : String
		{
			if (str == null)
				return null;
			var reg : RegExp;
			reg = new RegExp("([^/])(http://[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)" , "gi");
			str = str.replace(reg , "$1<a href=\"$2\" target=\"_blank\"><u>$2</u></a>");
			reg = new RegExp("^(http://[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)" , "gi");
			str = str.replace(reg , "<a href=\"$1\" target=\"_blank\"><u>$1</u></a>");
			reg = new RegExp("(http://[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)$" , "gi");
			str = str.replace(reg , "<a href=\"$1\" target=\"_blank\"><u>$1</u></a>");
			reg = new RegExp("([^/])(ftp://[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)" , "gi");
			str = str.replace(reg , "$1<a href=\"$2\" target=\"_blank\"><u>$2</u></a>");
			reg = new RegExp("^(ftp://[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)" , "gi");
			str = str.replace(reg , "<a href=\"$1\" target=\"_blank\"><u>$1</u></a>");
			reg = new RegExp("(ftp://[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)$" , "gi");
			str = str.replace(reg , "<a href=\"$1\" target=\"_blank\"><u>$1</u></a>");
			reg = new RegExp("([^/])(mms://[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)" , "gi");
			str = str.replace(reg , "$1<a href=\"$2\" target=\"_blank\"><u>$2</u></a>");
			reg = new RegExp("^(mms://[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)" , "gi");
			str = str.replace(reg , "<a href=\"$1\" target=\"_blank\"><u>$1</u></a>");
			reg = new RegExp("(mms://[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)$" , "gi");
			str = str.replace(reg , "<a href=\"$1\" target=\"_blank\"><u>$1</u></a>");
			reg = new RegExp("([a-z0-9_A-Z\\-\\.]{1,20})@([a-z0-9_\\-]{1,15})\\.([a-z]{2,4})" , "gi");
			str = str.replace(reg , "<a href=\"mailt$1@$2.$3\" target=\"_blank\"><u>$1@$2.$3</u></a>");
			reg = new RegExp("([^/])(www.[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)" , "gi");
			str = str.replace(reg , "$1<a href=\"http://$2\" target=\"_blank\"><u>$2</u></a>");
			reg = new RegExp("^(www.[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)" , "gi");
			str = str.replace(reg , "<a href=\"http://$1\" target=\"_blank\"><u>$1</u></a>");
			reg = new RegExp("(www.[A-Za-z0-9\\./=\\?%\\-&_~`@':+!]+)$" , "gi");
			str = str.replace(reg , "<a href=\"http://$1\" target=\"_blank\"><u>$1</u></a>");
			return str;
		}

	}
}